package cn.tedu.csmall.server.service.impl;

import cn.tedu.csmall.server.exception.ServiceException;
import cn.tedu.csmall.server.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.server.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.server.pojo.entity.AttributeTemplate;
import cn.tedu.csmall.server.service.IAttributeTemplateService;
import cn.tedu.csmall.server.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lixin
 * @Date 2022/9/13
 */
@Service
public class AttributeTemplateService implements IAttributeTemplateService {
    @Autowired
    private AttributeTemplateMapper mapper;

    @Override
    public void addNew(AttributeTemplateAddNewDTO attributeTemplate) {
        String name = attributeTemplate.getName();
        int count = mapper.countByName(name);
        if (count > 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "已有该属性");
        }
        //创建一个实体对象
        AttributeTemplate attributeTemplateEntity = new AttributeTemplate();
        //复制属性
        BeanUtils.copyProperties(attributeTemplate, attributeTemplateEntity);
        if (attributeTemplateEntity.getSort() == null) {
            attributeTemplateEntity.setSort(0);
        }
        final int row = mapper.insert(attributeTemplateEntity);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,"服务器繁忙");
        }
    }


}
